package com.itheima.socket.tcp.demo10;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerDemo01 {

    public static void main(String[] args) throws IOException {

        // 1. 创建ServerSocket对象
        ServerSocket serverSocket = new ServerSocket(8080) ;

        // 2. 监听客户端
        Socket socket = serverSocket.accept();

        // 3. 获取输入流对象
        InputStream inputStream = socket.getInputStream();

        // 4. 创建一个高效的字节输出流对象
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("F:\\a.avi")) ;

        // 5. 读取数据，写数据
        byte[] bytes = new byte[1024] ;
        int len = -1 ;
        while((len = inputStream.read(bytes)) != -1) {
            bufferedOutputStream.write(bytes , 0 , len);
        }

        // 6. 资源的释放
        bufferedOutputStream.close();
        socket.close();
        serverSocket.close();

    }

}
